#ifdef MU_LICENSING
#include "include/licensedialog.h"
#include "ui_licensedialog.h"
#include "include/Common.h"

#include <QNetworkInterface>
#include <QHostInfo>
#include <QDateTime>
#include "include/licensinguserinfo.h"
#include <QDomDocument>

#include <qca_core.h>
#include <qca_publickey.h>

#include <QDebug>


namespace
{

QString getOSName()
{
#if defined(_WIN32)
		return "Windows";
#elif defined(__APPLE_CC__)
		return "Mac";
#elif defined(__unix__)
		//QString str = "OS:Mac"; //looks like a typo
		return "Unix";
#else
		return "Unknown";
#endif
}

bool isLoopback(const QNetworkInterface& interface)
{
	return (interface.flags() & QNetworkInterface::IsLoopBack) == QNetworkInterface::IsLoopBack;
}

void getCurrentUserInfo(LicensingUserInfo* lui)
{
	lui->setHostName(QHostInfo::localHostName());
	lui->setOSName(getOSName());
	lui->setUserName(getUserName());
	lui->setTimeUtc(QDateTime::currentDateTimeUtc()); //not necessary for time is initialized by default

	//currently we are limiting amount of mac addresses to be not greater than 3
	//this is done to make license request shorter.
	//for more details look to "licensinguserinfo.h"

	lui->macAddresses().clear();
	foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces())
	{
		QString macAddress(interface.hardwareAddress().toLower());
		if (interface.hardwareAddress().isEmpty() == false && !isLoopback(interface))
		{
			QString macAddressString = QString("%1").arg(QString(macAddress.toAscii()));
			lui->macAddresses().push_back(macAddressString);

			if (lui->macAddresses().size() >= 3) //limitation described above
				break;
		}
	}
}

QString encryptXml(const QDomDocument& doc, QCA::PublicKey publicKey)
{
	QCA::SecureArray input(doc.toString(0).toUtf8());
	QCA::SecureArray output = publicKey.encrypt(input, QCA::EME_PKCS1_OAEP);
	return output.toByteArray().toHex();
}

} //private namespace



LicenseDialog::LicenseDialog(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::LicenseDialog)
{
	ui->setupUi(this);

//	QString email("");

//	{
//		QString str = "hostname:"+QHostInfo::localHostName();
//		email += str+"%0A";
//		ui->w_macAddresses->append(str);
//	}
//	{
//#if defined(_WIN32)
//		QString str = "OS:Windows";
//#elif defined(__APPLE_CC__)
//		QString str = "OS:Mac";
//#elif defined(__unix__)
//		//QString str = "OS:Mac"; //looks like a typo
//		QString str = "OS:Unix";
//#else
//		QString str = "OS:Unknown";
//#endif
//		email += str+"%0A";
//		ui->w_macAddresses->append(str);
//	}
//	{
//		QString str = "username:"+getUserName();
//		email += str+"%0A";
//		ui->w_macAddresses->append(str);
//	}
//	{
//		QString str = "utc:"+QDateTime::currentDateTimeUtc().toString();
//		email += str+"%0A";
//		ui->w_macAddresses->append(str);
//	}

//	foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces())
//	{
//		QString macAddress(interface.hardwareAddress().toLower());
//		if (interface.hardwareAddress().isEmpty() == false)
//		{
//			QString macAddressString = QString("%1").arg(QString(macAddress.toAscii()));
//			email += macAddressString+"%0A";
//			ui->w_macAddresses->append(macAddressString);
//		}
//	}

}

LicenseDialog::~LicenseDialog()
{
	delete ui;
}


void LicenseDialog::generateLicenseRequest()
{
	LicensingUserInfo lui;
	getCurrentUserInfo(&lui);
	QDomDocument doc;
	lui.writeTo(doc);

	//qDebug() << doc.toString(0);

	QString emailBody = encryptXml(doc, m_publicKey);

	ui->w_macAddresses->clear();
	ui->w_macAddresses->append(emailBody);

	ui->licensingEmail->setText("<html><body style=\" font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><u><a href=\"mailto:licensing@memusoft.com?Subject=Beta%20License%20Request&body=" + emailBody + "\"><span style=\" color:#0000ff;\">licensing@memusoft.com</span></a></u></p></body></html>");


	//qDebug() << doc.toString(4);
}


QString LicenseDialog::serialNumber()
{
	return ui->lineEdit->text();
}


QCA::PublicKey LicenseDialog::publicKey() const
{
	return m_publicKey;
}


void LicenseDialog::setPublicKey(const QCA::PublicKey& value)
{
	m_publicKey = value;
	generateLicenseRequest();
}
#endif
